package com.qdxwx.manage.controller.course;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.enums.BusinessType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * 章节新增保存Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/node")
public class CourseNodeEditController extends BaseController {

    @Autowired
    private CourseNodeService courseNodeService;
    @Autowired
    private CourseService courseService;


    @GetMapping("/folder/edit.html")
    public String folderEdit(HttpServletRequest request, ModelMap mmap) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if(courseId < 1 ) return error500;
        int nodeId = TKConvert.toInteger(request.getParameter("nodeId"));
        CourseNode courseNode;
        if(nodeId > 0){
            courseNode = courseNodeService.get(nodeId);
            if (courseNode == null || courseNode.getCourseId() != courseId){
                return error404;
            }
        }else {
            courseNode = new CourseNode();
            courseNode.setCourseId(courseId);
        }
        mmap.put("courseNode", courseNode);
        return "course/node-folder-edit";
    }

    @PostMapping("/folder/save.json")
    @Permissions("course:edit")
    @Log(title = "编辑课程章节目录", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult folderSave(HttpServletRequest request) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        int sort = TKConvert.toInteger(request.getParameter("sort"));
        if(courseId < 1 ) return error("主课程id参数异常");
        int nodeId = TKConvert.toInteger(request.getParameter("nodeId"));
        String name = request.getParameter("name");
        if(TKString.isEmpty(name)) return error("目录名称不能为空");
        int row;
        CourseNode courseNode;
        if(nodeId > 0){
            courseNode = courseNodeService.get(nodeId);
            if (courseNode == null || courseNode.getCourseId() != courseId){
                return error("数据异常");
            }
            courseNode.setName(name);
            courseNode.setSort(sort);
            row = courseNodeService.update(courseNode);
        }else {
            courseNode = new CourseNode();
            courseNode.setCourseId(courseId);
            courseNode.setName(name);
            courseNode.setSort(sort);
            row = courseNodeService.insert(courseNode);
        }
        return row > 0 ? success():error();
    }

    /**
     * 新增知识点的基本信息
     */
    @GetMapping("/add.html")
    public String add(HttpServletRequest request, ModelMap mmap) {
        int parentId = TKConvert.toInteger(request.getParameter("parentId"));
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        String sellType = request.getParameter("sellType");
        if(courseId < 1 || TKString.isEmpty(sellType)) return error500;
        mmap.put("parentId", parentId);
        mmap.put("courseId", courseId);
        mmap.put("sellType", sellType);
        return "course/node-add";
    }

    /**
     * 修改知识点的基本信息
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int nodeId = TKConvert.toInteger(request.getParameter("id"));
        if (nodeId < 1) return error404;
        String sellType = request.getParameter("sellType");
        if (TKString.isEmpty(sellType)) return error404;
        CourseNode courseNode = courseNodeService.get(nodeId);
        if (courseNode == null) return error404;
        mmap.put("courseNode", courseNode);
        mmap.put("sellType", sellType);
        return "course/node-edit";
    }


    /**
     * 新增保存知识点的基本信息
     */
    @PostMapping("/save.json")
    @Permissions("course:edit")
    @Log(title = "编辑课程章节", businessType = BusinessType.UPDATE)
    @ResponseBody
    public AjaxResult courseNodeSave(HttpServletRequest request) {
        int nodeId = TKConvert.toInteger(request.getParameter("nodeId"));
        CourseNode courseNode;
        if(nodeId > 0){
            courseNode = courseNodeService.get(nodeId);
            if(courseNode == null) return error("找不到该章节");
        }else {
            courseNode = new CourseNode();
            int parentId = TKConvert.toInteger(request.getParameter("parentId"));
            int courseId = TKConvert.toInteger(request.getParameter("courseId"));
            if(parentId < 1 || courseId < 1) return error("找不到对应上级ID");
            courseNode.setParentId(parentId);
            courseNode.setCourseId(courseId);
            courseNode.setNodeType(1);
        }
        courseNode.setFree(TKConvert.toInteger(request.getParameter("free"),2));
        String name = request.getParameter("name");
        if(TKString.isEmpty(name)) return error("标题不能为空");
        courseNode.setName(name);
        String sellType = request.getParameter("sellType");
        String fileType = request.getParameter("fileType");
        if("LIVE".equals(sellType)) fileType = sellType;
        if(TKString.isEmpty(fileType)) return error("请选择类型");
        courseNode.setFileType(fileType);
        courseNode.setNodeType(1);
        courseNode.setSort(TKConvert.toInteger(request.getParameter("sort")));
        String videoType = request.getParameter("videoType");
        String nodeKey = request.getParameter("nodeKey");
        String content = request.getParameter("content");

        switch (fileType){
            case "VIDEO":
                if(TKString.isEmpty(videoType)) return error("请选择视/音频服务商");
                if(TKString.isEmpty(nodeKey)) return error("视/音频id/地址不能为空");
                courseNode.setVideoType(videoType);
                courseNode.setDurations(TKConvert.toInteger(request.getParameter("durations")));
                courseNode.setNodeKey(nodeKey);
                break;
            case "AUDIO":
                if(TKString.isEmpty(videoType)) return error("请选择视/音频服务商");
                if(TKString.isEmpty(nodeKey)) return error("视/音频id/地址不能为空");
                courseNode.setVideoType(videoType);
                courseNode.setNodeKey(nodeKey);
                break;
            case "TXT":
                if(TKString.isEmpty(content)) return error("文本内容不能为空");
                courseNode.setContent(content);
                break;
            case "LIVE":
                LocalDateTime liveBeginTime = TKConvert.toLocalDateTime(request.getParameter("liveBeginTime"));
                LocalDateTime liveEndTime = TKConvert.toLocalDateTime(request.getParameter("liveEndTime"));
                if(TKString.isEmpty(nodeKey)) return error("视/音频id/地址不能为空");
                String supplier = request.getParameter("supplier");
                if(TKString.isEmpty(supplier)) supplier = "BAIJIAYUN";
                courseNode.setFileType(fileType);
                courseNode.setVideoType("BAIJIAYUN");
                courseNode.setSupplier(supplier);
                courseNode.setNodeKey(nodeKey);
                courseNode.setLiveBeginTime(liveBeginTime);
                courseNode.setLiveEndTime(liveEndTime);
                break;
            default:
                return error();
        }
        int row;
        if(nodeId > 0){
           row = courseNodeService.update(courseNode);
        }else {
           row = courseNodeService.insert(courseNode);
        }
        if(row > 0){
            int totals = courseNodeService.getDurationSum(courseNode.getCourseId());
            courseService.updateTotalDurations(courseNode.getCourseId(),totals);
        }
        return toAjax(row);
    }

    /**
     * 删除
     */
    @Permissions("course:remove")
    @Log(title = "删除课程章节", businessType = BusinessType.DELETE)
    @GetMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int nodeId = TKConvert.toInteger(request.getParameter("id"));
        if(nodeId < 1) return error();
        return toAjax(courseNodeService.delete(nodeId));
    }
}
